﻿
using System;
using Test30.Util;
using UnityEngine;

namespace Assets.Scripts.Extensions
{
    public static class UtilExtensions
    {
        public static void ListenTo<TSender, TArgs>(this MonoBehaviour gameObject, IEvent<TSender, TArgs> eventObj,
            Action<TSender, TArgs> handler)
        {
            eventObj.Add(handler);
            Action callback = ()=>eventObj.Remove(handler);
            callback();
        }

        public static Transform Create(Transform parentTrans, string name, Vector3 pos = default(Vector3))
        {
            GameObject go = new GameObject(name) {name = name};
            Transform trans = go.transform;
            if (parentTrans != null) trans.SetParent(parentTrans);
            trans.localPosition = pos;
            trans.localScale = Vector3.one;
            trans.localRotation = Quaternion.identity;
            return trans;
        }

    }
}
